﻿package {
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Graphics;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	import flash.events.*;
	
	public class PlaySlider extends MovieClip {
		
		var bar:Sprite;
		var posBox:Sprite;
		
		var rectHeight:int = 10;
		var rectWidth:int = 5;
		var posBoxX:int;
		var slideCounter:int;
		private var timer:Timer;

		var xCoord:int;
		var yCoord:int;
		
		public function PlaySlider(xCoord:int,yCoord:int,barLength:int) {
			bar = new Sprite();
			bar.graphics.lineStyle(5,0xFF0000);
			bar.graphics.moveTo(xCoord,yCoord);
			bar.graphics.lineTo(xCoord+barLength,yCoord);
			this.addChild(bar);
			
			posBoxX = xCoord+2;
			this.xCoord = xCoord+2;
			this.yCoord = yCoord;
			
			posBox = new Sprite();
			posBox.graphics.lineStyle(rectWidth,0x000000);
			posBox.graphics.moveTo(posBoxX,yCoord+rectHeight/2);
			posBox.graphics.lineTo(posBoxX,yCoord-rectHeight/2);
			this.addChild(posBox);
		}
		
		public function updateSlider(newX:int):void{
			posBoxX = xCoord+newX;
			posBox.graphics.moveTo(posBoxX,yCoord+rectHeight/2);
			posBox.graphics.lineTo(posBoxX,yCoord-rectHeight/2);
			trace(posBoxX);
		}
		
		public function resetSlider(){
			posBoxX = xCoord+2;
			posBox.graphics.moveTo(posBoxX,yCoord+rectHeight/2);
			posBox.graphics.lineTo(posBoxX,yCoord-rectHeight/2);
		}
		
		public function startSlider(length:int):void{
			slideCounter = 0;
			timer = new Timer(1000,length/1000);
			trace("Nu kører slideren med " + 1000/length*100 + " i " +
				   length/1000 + "sekunder");
			timer.addEventListener(TimerEvent.TIMER,
								   function(event2:TimerEvent):void{
										slideCounter++;
										updateSlider((slideCounter*100*1000)/length);
								   });
			timer.start();
		}
	}
}